package com.freesky.biz.abchina.controller;

import com.freesky.biz.abchina.api.AbChinaCoupon;
import com.freesky.biz.abchina.domain.TElevenRecord;
import com.freesky.biz.abchina.domain.TMaotaiCountRecord;
import com.freesky.biz.abchina.domain.TMaotaiRecord;
import com.freesky.biz.abchina.domain.TMaotaiShare;
import com.freesky.biz.abchina.service.IElevenRecordService;
import com.freesky.biz.abchina.service.IMaotaiRecordService;
import com.freesky.common.annotation.Log;
import com.freesky.common.core.controller.BaseController;
import com.freesky.common.core.domain.AjaxResult;
import com.freesky.common.core.page.TableDataInfo;
import com.freesky.common.enums.BusinessType;
import com.freesky.common.exception.CustomException;
import com.freesky.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@RequestMapping("/abchina/eleven/")
public class ElevenRecordController extends BaseController {

    @Autowired
    private IElevenRecordService elevenRecordService;

    @Autowired
    private AbChinaCoupon abChinaCoupon;
    /**
     * 双十一活动列表
     */
    @PreAuthorize("@ss.hasPermi('abchina:eleven:list')")
    @GetMapping("/list")
    public TableDataInfo list(TElevenRecord elevenRecord) {
        startPage();
        if(StringUtils.isNotEmpty(elevenRecord.getMobile())
                && StringUtils.isEmpty(elevenRecord.getCustId())){
            AjaxResult result = abChinaCoupon.getCustIdByPhone(elevenRecord.getMobile());
            if(result.isSuccess()){
                elevenRecord.setCustId(result.get("data").toString());
            } else {
                throw new CustomException(result.getMsg());
            }
        }

        List<TElevenRecord> list = elevenRecordService.findList(elevenRecord);
        return getDataTable(list);
    }

    @Log(title = "双十一活动", businessType = BusinessType.EXPORT)
    @PreAuthorize("@ss.hasPermi('abchina:eleven:export')")
    @GetMapping("/export")
    public AjaxResult export(TElevenRecord elevenRecord,
                             HttpServletResponse response) {
        return elevenRecordService.exportFile(elevenRecord);
    }
}
